﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for UserDAO
/// </summary>
public class UserDAO:BaseDBConnect
{
    
    public UserDAO()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public int Create(User user)
    {
        SqlConnection con = null;
        try
        {
            con = new SqlConnection(connString);
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_createUser");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("userPassword", SqlDbType.VarChar);
            cmd.Parameters.Add("firstName", SqlDbType.VarChar);
            cmd.Parameters.Add("lastName", SqlDbType.VarChar);
            cmd.Parameters.Add("address", SqlDbType.VarChar);
            cmd.Parameters.Add("phone", SqlDbType.VarChar);
            cmd.Parameters.Add("email", SqlDbType.VarChar);
            cmd.Parameters.Add("birth", SqlDbType.VarChar);
            cmd.Parameters.Add("genre", SqlDbType.VarChar);
            //
            cmd.Parameters["userPassword"].Value = user.UserPassword;
            cmd.Parameters["firstName"].Value = user.FirstName;
            cmd.Parameters["lastName"].Value = user.LastName;
            cmd.Parameters["address"].Value = user.Address;
            cmd.Parameters["phone"].Value = user.Phone;
            cmd.Parameters["email"].Value = user.Email;
            cmd.Parameters["birth"].Value = user.Birth;
            cmd.Parameters["genre"].Value = user.Genre;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine("UserDao.Create error: " + ex.Message.ToString());
            return -1;
        }
        finally
        {
            if (con.State != ConnectionState.Closed)
            {
                con.Close();
            }
        }
    }
    public List<User> readAll()
    {
        List<User> list = new List<User>();
        SqlConnection con = null;
        try
        {
            con = getCon();
            SqlCommand cmd = new SqlCommand("sp_selectAllUser1");
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader sr = cmd.ExecuteReader();
            while (sr.Read())
            {
                User u = new User();
                u.UserName = sr.GetString(sr.GetOrdinal("UserID"));
                u.UserPassword = sr.GetString(sr.GetOrdinal("userPassword"));
                u.FirstName = sr.GetString(sr.GetOrdinal("firstName"));
                u.LastName = sr.GetString(sr.GetOrdinal("lastName"));
                u.Address = sr.GetString(sr.GetOrdinal("address"));
                u.Phone = sr.GetString(sr.GetOrdinal("phone"));
                u.Email = sr.GetString(sr.GetOrdinal("email"));
                u.Birth = sr.GetDateTime(sr.GetOrdinal("birth"));
                u.Genre = sr.GetString(sr.GetOrdinal("genre"));
                list.Add(u);
            }
            return list;
        }
        catch (Exception ex)
        {
            Console.WriteLine("UserDao.Create error: " + ex.Message.ToString());
            return null;
        }
        finally
        {
            closeCon(con);
        }
    }
}